package com.yonyou.component;

import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.MessageType;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.ui.popup.Balloon;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.wm.StatusBar;
import com.intellij.openapi.wm.WindowManager;
import com.intellij.ui.awt.RelativePoint;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

public class PluginDemoProcess extends Task.Backgroundable {



    private Project project;

    public PluginDemoProcess(@Nullable Project project, @Nls(capitalization = Nls.Capitalization.Title) @NotNull String title) {
        super(project, title);
        this.project = project;
    }

    @Override
    public void run(@NotNull ProgressIndicator progressIndicator) {
        // 设置进度条为模糊进度条
        progressIndicator.setIndeterminate(true);
        try {
            // 这里就是我们需要模拟执行我们相关的业务逻辑的部分，此处就使线程睡眠3秒作为模拟
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 开启进度条
        progressIndicator.setIndeterminate(true);
        // 设置进度条为百分百
        progressIndicator.setFraction(1.0);
        //
        progressIndicator.setText("插件执行结束1");
        // 在状态栏提示信息【代码生成完成】
        StatusBar statusBar = WindowManager.getInstance().getStatusBar(project);
        JBPopupFactory.getInstance()
                .createHtmlTextBalloonBuilder("插件执行结束2", MessageType.INFO, null)
                .setFadeoutTime(7500)
                .createBalloon()
                .show(RelativePoint.getCenterOf(statusBar.getComponent()), Balloon.Position.atRight);
        // 弹出模态框显示以下的消息
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                Messages.showMessageDialog("插件执行结束3", "提示", null);
            }
        });
    }
}
